#include "roletable.h"
#include "ui_roletable.h"
#include "util.h"
#include "roletableitem.h"
#include "edituserdialog.h"
#include "toast.h"
#include "model/datacenter.h"

RoleTable::RoleTable(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::RoleTable)
{
    ui->setupUi(this);

    ui->userStatus->addItem("全部分类");
    ui->userStatus->addItem("启⽤");
    ui->userStatus->addItem("停⽤");
    ui->userStatus->setCurrentIndex(0);

    // 顶部对齐
    ui->layout->setAlignment(Qt::AlignmentFlag::AlignTop);

    paginator = new Paginator(20, ui->paginatorArea);
    paginator->move(0, 15);
    paginator->show();

    // 限制编辑框只能输⼊⼿机号
    QRegularExpression regExp("^1\\d{10}$");
    QValidator* validator = new QRegularExpressionValidator(regExp, this);
    ui->phone->setValidator(validator);

    // 更新管理员项
    updateRoleTable();

    // 关联样式
    initStyleSheet();

    // 链接信号槽
    connect(ui->resetBtn, &QPushButton::clicked, this, &RoleTable::onRestBtnClicked);
    connect(ui->queryBtn, &QPushButton::clicked, this, &RoleTable::onQueryBtnClicked);
    connect(ui->insertBtn, &QPushButton::clicked, this, &RoleTable::onInsertBtnClicked);

    // 获取管理员信息--通过手机号
    auto dataCenter = model::DataCenter::getInstance();
    connect(dataCenter, &model::DataCenter::getAdminInfoByPhoneDone, this, &RoleTable::updateRoleTable);

    // 获取管理员信息--通过状态
    connect(dataCenter, &model::DataCenter::getAdminInfoByStatusDone, this, &RoleTable::updateRoleTable);

    // 新增管理员
    connect(dataCenter, &model::DataCenter::newAdminDone, this, [=]{
        getAdminList(1);
    });

    // 删除管理员成功
    connect(dataCenter, &model::DataCenter::delAdminDone, this, [=]{
        getAdminList(1);
    });
}

RoleTable::~RoleTable()
{
    delete ui;
}

void RoleTable::initStyleSheet()
{
    styleSheet.insert("选中","background-color:#3ECEFF;"
                              "border-radius : 4px;"
                              "font-family : 微软雅黑;"
                              "font-size : 14px;"
                              "color : #FFFFFF;");
    styleSheet.insert("未选中", "background-color : #FFFFFF;"
                                "border-radius : 4px;"
                                "border: 1px solid #DCDEE0;"
                                "font-family : 微软雅黑;"
                                "font-size : 14px;"
                                "color : #222222;");
}

void RoleTable::updateRoleTable()
{
    // 1. 清空界面中旧的管理员信息
    QLayoutItem* item = nullptr;
    while(nullptr != (item = ui->layout->takeAt(0))){
        delete item->widget();
        delete item;
    }

    // 2. 获取管理员列表
    auto dataCenter = model::DataCenter::getInstance();
    auto adminListPtr = dataCenter->getAdminList();
    if(nullptr == adminListPtr){
        return;
    }

    // 3. 重置分页器
    auto adminList = adminListPtr->adminInfos;
    int adminCountPage = model::AdminList::PAGE_COUNT;
    int totalCount = adminListPtr->totalCount;
    if(1 == page){
        resetPaginator((totalCount+adminCountPage-1)/adminCountPage);
    }

    // 4. 将管理员信息添加到界面中
    int seqNum = (page - 1)*adminCountPage + 1;
    for(int i = 0; i < adminList.size(); ++i){
        RoleTableItem* roleTableItem = new RoleTableItem(this, adminList[i], seqNum++);
        ui->layout->addWidget(roleTableItem);
    }
}

void RoleTable::getAdminList(int page)
{
    this->page = page;
    auto dataCenter = model::DataCenter::getInstance();
    auto adminList = dataCenter->getAdminList();
    adminList->adminInfos.clear();

    // 如果手机号不空，优先按照手机号获取管理员信息，否则按照状态获取
    QString phoneNumber = ui->phone->text();
    if(!phoneNumber.isEmpty()){
        if(phoneNumber.size() != 11){
            Toast::showMessage("输入手机号有误，请重新输入!!!");
            return;
        }

        dataCenter->getAdminInfoByPhoneAsync(phoneNumber);
    }else{
        // 通过状态获取管理员信息
        model::AdminStatus adminStatus = static_cast<model::AdminStatus>(ui->userStatus->currentIndex());
        dataCenter->getAdminInfoByStatusAsync(page, adminStatus);
    }
}

void RoleTable::resetPaginator(int pageCount)
{
    if(paginator){
        delete paginator;
    }

    paginator = new Paginator(pageCount, ui->paginatorArea);
    paginator->move(0, 15);
    paginator->show();

    // 分页器信号
    connect(paginator, &Paginator::pageChanged, this, [=](int page){
        getAdminList(page);
    });
}

void RoleTable::onRestBtnClicked()
{
    // 让重置按钮高亮，查询按钮非高亮
    ui->resetBtn->setStyleSheet(styleSheet["选中"]);
    ui->queryBtn->setStyleSheet(styleSheet["未选中"]);
    ui->phone->setText("");
    ui->userStatus->setCurrentIndex(0);

    // 获取管理员列表
    auto dataCenter = model::DataCenter::getInstance();
    auto myselfInfo = dataCenter->getMyselfInfo();
    if(myselfInfo->isAdminDisable()){
        Toast::showMessage("您已被禁止，无法进行操作!!!");
    }else{
        getAdminList(1);
    }
}

void RoleTable::onQueryBtnClicked()
{
    // 让重置按钮非高亮，查询按钮高亮
    ui->resetBtn->setStyleSheet(styleSheet["未选中"]);
    ui->queryBtn->setStyleSheet(styleSheet["选中"]);

    // 获取管理员列表
    auto dataCenter = model::DataCenter::getInstance();
    auto myselfInfo = dataCenter->getMyselfInfo();
    if(myselfInfo->isAdminDisable()){
        Toast::showMessage("您已被禁止，无法进行操作!!!");
    }else{
        LOG()<<"查询按钮点击";
        getAdminList(1);
    }

}

void RoleTable::onInsertBtnClicked()
{
    model::AdminInfo adminInfo;
    EditUserDialog* editAdminDlg = new EditUserDialog(nullptr, "新增后台用户", adminInfo);
    editAdminDlg->setPhoneEditReadOnly(false);
    editAdminDlg->exec();
    if(editAdminDlg->getCommitResult()){
        // 如果用户点击了提交按钮，需要向服务器发送新增管理员信息的请求
        auto dataCenter = model::DataCenter::getInstance();
        dataCenter->newAdminAsync(adminInfo);
    }

    delete editAdminDlg;
}
